package com.lczc.soa.controller;


import com.lczc.soa.domain.Question;
import com.lczc.soa.domain.SearchQuestionResponse;
import com.lczc.soa.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/Question")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @RequestMapping("/toadd")
    public String addquesetion(Model model){
        model.addAttribute("question",new Question());
        return "idea_add";
    }
    @RequestMapping("/deal")
    public String dealQuestion(@RequestParam("id") String id, Model model){
        Question question = questionService.findQuestionByPrimaryKey(id);
        model.addAttribute("question",question);
        return "deal";
    }

    @RequestMapping("/edit")
    private String editQuestion(Question question){
        int updated = questionService.savaQuestion(question);
        return "redirect:/Question/questionlist";
    }

    @RequestMapping("/add")
    public  String addQuestion(Question question, HttpServletRequest request){
        question.setQuestionName(request.getAttribute("questionName").toString());
        question.setContent(request.getAttribute("content").toString());
        int upate= questionService.savaQuestion(question);
        return "redirect:/Question/toadd";

    }

    @RequestMapping("/newadd")
    public  String d(Question question){
        int upate= questionService.savaQuestion(question);
        return "redirect:/Question/questionlist";

    }



    @RequestMapping("/questionlist")
    public  String questionList(){   return "question_list";
    }


    @GetMapping("/api/search")
    @ResponseBody
    public SearchQuestionResponse searchQuestionResponse(@RequestParam("pageNum") Integer pageNum,
                                                   @RequestParam("pageSize") Integer pageSize) {
        return questionService.findAllQuestion(pageNum, pageSize);
    }





    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchQuestionResponse searchQuestionResponseLike(@RequestParam("keywords") String keywords,
                                                                   @RequestParam("pageNum") Integer pageNum,
                                                                   @RequestParam("pageSize") Integer pageSize) {
        return questionService.findQuestionLikeName(keywords, pageNum, pageSize);
    }

    @RequestMapping("/list")
    public  String GetQuestion(Model model){
        List<Question> questions=questionService.listQuestion();
        model.addAttribute("questions",questions);
        return  "deal_list";
    }
    @RequestMapping("/content")
    public String QuestionContent(@RequestParam("id") String id,Model model){
        Question question = questionService.findQuestionByPrimaryKey(id);
        model.addAttribute("question",question);
        return "question_content";
    }




}
